package com.jintian.smart.platform.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.jintian.smart.kernel.common.controller.SupportBaseController;
import com.jintian.smart.kernel.common.util.SmartEnumUtil;
import com.jintian.smart.kernel.core.constant.SwaggerTagConst;
import com.jintian.smart.kernel.core.domain.ResponseDTO;
import com.jintian.smart.kernel.core.support.SerialNumberIdEnum;
import com.jintian.smart.base.api.system.entity.SerialNumberEntity;
import com.jintian.smart.base.api.system.form.SerialNumberGenerateForm;
import com.jintian.smart.base.api.system.entity.SerialNumberRecordEntity;
import com.jintian.smart.base.api.system.form.SerialNumberRecordQueryForm;
import com.jintian.smart.platform.system.mapper.SerialNumberMapper;
import com.jintian.smart.platform.system.service.SerialNumberRecordService;
import com.jintian.smart.kernel.core.support.SerialNumberService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.beetl.sql.core.page.PageResult;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 单据序列号
 */
@Tag(name = SwaggerTagConst.Support.SERIAL_NUMBER)
@RestController
@RequestMapping("/sys/serialNumber")
public class SerialNumberController extends SupportBaseController {

    @Resource
    private SerialNumberMapper serialNumberMapper;

    @Resource
    private SerialNumberService serialNumberService;

    @Resource
    private SerialNumberRecordService serialNumberRecordService;

    @Operation(summary = "生成单号")
    @PostMapping("/generate")
    @SaCheckPermission("support:serialNumber:generate")
    public ResponseDTO<List<String>> generate(@RequestBody @Valid SerialNumberGenerateForm generateForm) {
        SerialNumberIdEnum serialNumberIdEnum = SmartEnumUtil.getEnumByValue(generateForm.getSerialNumberId(), SerialNumberIdEnum.class);
        if (null == serialNumberIdEnum) {
            return ResponseDTO.userErrorParam("SerialNumberId，不存在" + generateForm.getSerialNumberId());
        }
        return ResponseDTO.ok(serialNumberService.generate(serialNumberIdEnum, generateForm.getCount()));
    }

    @Operation(summary = "获取所有单号定义")
    @GetMapping("/all")
    public ResponseDTO<List<SerialNumberEntity>> getAll() {
        return ResponseDTO.ok(serialNumberMapper.all());
    }

    @Operation(summary = "获取生成记录")
    @PostMapping("/queryRecord")
    @SaCheckPermission("support:serialNumber:record")
    public ResponseDTO<PageResult<SerialNumberRecordEntity>> queryRecord(@RequestBody @Valid SerialNumberRecordQueryForm queryForm) {
        return ResponseDTO.ok(serialNumberRecordService.query(queryForm));
    }
}
